package com.sjgame.quartz;

import com.sjgame.quartz.job.MySchedulerJob2;
import com.sjgame.util.SpringUtil;
import org.quartz.*;
import org.quartz.impl.StdScheduler;
import org.springframework.context.ApplicationContext;
import org.springframework.stereotype.Component;

/**
 * @program: shgame
 * @description:
 * @author: zcl
 * @create: 2018-06-29 15:58
 **/

@Component
public class MyScheduler {


    public void schedulerJob() throws SchedulerException {
        ApplicationContext annotationContext = SpringUtil.getApplicationContext();
        StdScheduler stdScheduler = (StdScheduler) annotationContext.getBean("mySchedulerFactoryBean");//获得上面创建的bean
        Scheduler myScheduler = stdScheduler;
        startScheduler1(myScheduler);

        myScheduler.start();
    }

    public void startScheduler1(Scheduler scheduler) throws SchedulerException {
        JobDetail jobDetail = JobBuilder.newJob(MySchedulerJob2.class).withIdentity("job1", "jobGroup1").build();
        CronScheduleBuilder cronScheduleBuilder = CronScheduleBuilder.cronSchedule("0/5 * * * * ?");
        CronTrigger trigger = TriggerBuilder.newTrigger().withIdentity("trigger1", "triggerGroup1")
                .withSchedule(cronScheduleBuilder).build();
        scheduler.scheduleJob(jobDetail, trigger);
    }



}